package com.anstudio.app.lhasa.service.workers;

import com.anstudio.app.lhasa.core.daos.SightDao;
import com.anstudio.app.lhasa.core.daos.SightImgDao;
import com.anstudio.app.lhasa.core.modules.Sight;
import com.anstudio.app.lhasa.service.modules.SightInformation;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import static com.anstudio.app.lhasa.service.modules.SightInformation.DEFAULT_SIGHTINFO;

public enum SightsWorker {
    instance;

    public List<SightInformation> getAllSightInformation() {
        List<SightInformation> ret = new ArrayList<>();
        List<Sight> sights = SightDao.getDao().queryAllSights();

        sights.parallelStream()
                .forEach(sight -> ret.add(new SightInformation()
                        .setSight(sight).setSightImageUrls(SightImgDao.getDao().queryImgsBySightId(sight.getId()))));
        return ret;
    }

    public SightInformation getSightInformationByGPS(float latitude, float longitude) {
        SightInformation sightInformation = DEFAULT_SIGHTINFO;
        Optional sight = SightDao.getDao().querySightByGPS(latitude, longitude);
        if (sight.isPresent()) {
            Sight sightContent = (Sight) sight.get();
            sightInformation.setSight(sightContent);
            sightInformation.setSightImageUrls(SightImgDao.getDao().queryImgsBySightId(sightContent.getId()));
        }

        return sightInformation;
    }

    public SightInformation getSightInformationById(int id) {
        SightInformation sightInformation = DEFAULT_SIGHTINFO;
        Optional sight = SightDao.getDao().querySightById(id);
        if (sight.isPresent()) {
            Sight sightContent = (Sight) sight.get();
            sightInformation.setSight(sightContent);
            sightInformation.setSightImageUrls(SightImgDao.getDao().queryImgsBySightId(sightContent.getId()));
        }

        return sightInformation;
    }

    public SightInformation getSightIngotmationByName(String name) {
        SightInformation sightInformation = DEFAULT_SIGHTINFO;
        Optional sight = SightDao.getDao().querySightByName(name);
        if (sight.isPresent()) {
            Sight sightContent = (Sight) sight.get();
            sightInformation.setSight(sightContent);
            sightInformation.setSightImageUrls(SightImgDao.getDao().queryImgsBySightId(sightContent.getId()));
        }

        return sightInformation;
    }
}
